#!/usr/bin/env python3

import pandas as pd

# Read the CSV file
df = pd.read_csv('kernel_passes.csv')

# Create a Pandas Excel writer using XlsxWriter as the engine
with pd.ExcelWriter('kernel_passes.xlsx', engine='openpyxl') as writer:
    # Loop through each unique value in the 'Example' column
    for example in sorted(df['Example'].unique()):
        # Filter the DataFrame for the current example
        example_df = df[df['Example'] == example]
        # Drop the 'Example' column
        example_df = example_df.drop(columns=['Example', 'Passes'])
        # Write the DataFrame to a separate sheet
        example_df.to_excel(writer, sheet_name=example, index=False)

        # Get the current workbook and the sheet
        workbook = writer.book
        worksheet = writer.sheets[example]

        # Adjust the width of the columns
        # for column in worksheet.columns:
        #   max_length = 0
        #   column = [cell for cell in column]
        #   for cell in column:
        #     try:
        #       if len(str(cell.value)) > max_length:
        #         max_length = len(str(cell.value))
        #       # Enable text wrapping
        #       cell.alignment = Alignment(wrap_text=True)
        #     except:
        #       pass
        #   adjusted_width = (max_length + 2)  # Adding some extra space
        #   worksheet.column_dimensions[column[0].column_letter].width = adjusted_width

        # Adjust the height of the rows
        # for row in worksheet.iter_rows():
        #     max_height = 0
        #     for cell in row:
        #       if cell.value:
        #         # Calculate the number of lines based on line breaks
        #         line_count = str(cell.value).count('\n') + 1
        #         # Set row height based on the number of lines
        #         max_height = max(max_height, line_count * 15)  # Adjust the multiplier for height as needed
        #     worksheet.row_dimensions[row[0].row].height = max_height

print("Sheets created successfully.")
